package searchkeyword

import (
	"context"
	"strings"
	"time"

	"mall/common/modelx"
	"mall/service/backend/common/errorx"
	"mall/service/backend/common/i18n"
	"mall/service/backend/rpc/internal/svc"
	"mall/service/backend/rpc/internal/utils/errorhandler"
	"mall/service/backend/rpc/types/backend"

	"github.com/volatiletech/null/v8"
	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateSearchKeywordLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateSearchKeywordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateSearchKeywordLogic {
	return &UpdateSearchKeywordLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateSearchKeywordLogic) UpdateSearchKeyword(in *backend.SearchKeywordInfo) (*backend.BaseResp, error) {
	if len(in.Ids) > 0 && in.Status != nil { // 批量修改状态
		tx, err := l.svcCtx.DB.BeginTx(l.ctx, nil)
		if err != nil {
			return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
		}

		now := time.Now()
		for _, id := range in.Ids {
			if id > 0 {
				searchKeyword, err := l.svcCtx.SearchKeyword.FindOneNoCache(l.ctx, tx, id)
				if err != nil {
					tx.Rollback()
					return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
				}

				searchKeyword.Status = uint8(*in.Status)
				searchKeyword.UpdatedAt = now
				err = l.svcCtx.SearchKeyword.Update(l.ctx, tx, searchKeyword)
				if err != nil {
					tx.Rollback()
					return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
				}
			}
		}

		tx.Commit()

		return &backend.BaseResp{
			Msg: i18n.UpdateSuccess,
		}, nil
	}

	if in.Id == nil || *in.Id == 0 {
		return nil, errorx.NewInvalidArgumentError(i18n.ValidationError)
	}

	searchKeyword, err := l.svcCtx.SearchKeyword.FindOneNoCache(l.ctx, nil, *in.Id)
	if err != nil {
		return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
	}

	if in.Type != nil {
		searchKeyword.Type = uint8(*in.Type)
	}
	if in.Title != nil {
		searchKeyword.Title = strings.TrimSpace(*in.Title)
	}
	if in.LinkType != nil {
		searchKeyword.LinkType = uint8(*in.LinkType)
	}
	if in.LinkUrl != nil {
		searchKeyword.LinkURL = strings.TrimSpace(*in.LinkUrl)
	}
	if in.ObjectType != nil {
		searchKeyword.ObjectType = uint16(*in.ObjectType)
	}
	if in.ObjectId != nil {
		searchKeyword.ObjectID = *in.ObjectId
	}
	if in.Status != nil {
		searchKeyword.Status = uint8(*in.Status)
	}
	if in.Sort != nil {
		searchKeyword.Sort = uint16(*in.Sort)
	}
	searchKeyword.UpdatedAt = time.Now()
	if in.StartedAt != nil {
		if localTime, err := modelx.ParseLocalTime(*in.StartedAt); err == nil {
			searchKeyword.StartedAt = null.TimeFrom(localTime)
		}
	}
	if in.EndedAt != nil {
		if localTime, err := modelx.ParseLocalTime(*in.EndedAt); err == nil {
			searchKeyword.EndedAt = null.TimeFrom(localTime.Add(time.Minute * -1))
		}
	}

	err = l.svcCtx.SearchKeyword.Update(l.ctx, nil, searchKeyword)
	if err != nil {
		return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
	}

	return &backend.BaseResp{
		Msg: i18n.UpdateSuccess,
	}, nil
}
